import {list,summaryList} from '../../../../api/point'
const util = require('../../../../utils/util.js')
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pageNum: 1,
    pageSize: 20,
    detailedList: [],
    today:'',
    date:'',
    choseData:"",
    startDate:'',
    endDate:'',
    VstartDate:'',
    VendDate:'',
    tab:[{index:1,title:"今日",fields:'day',checked:true},{index:2,title:"月度",fields:'month'},{index:3,title:"年度",fields:'year'}],
    arrType:[{id:1,value:"收入"},{id:2,value:"支出"}],
    currTabName:"收入",
    fields:'day',
    type:'',
    amount:0,
    total:0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.intDate()
  },

  /**z
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.pageNum=1,
    this.data.pageSize=20,
    this.loadSummary()
  },
  intDate(){
    var date =new Date()
    this.data.today = date
    this.data.date = util.datetime(date)
    this.data.choseData = util.formatData(this.data.fields,date)
    this.data.icon = util.thumbnail('https://rzico-weex.oss-cn-hangzhou.aliyuncs.com/45082/income.png',80,80)
    this.setData({
      type:"1",
      fields:"day",      
      tab:this.data.tab,
      today:this.data.today,
      startDate:this.data.date,
      endDate:this.data.date,
      VstartDate:util.formatData(this.data.fields,date),
      VendDate:util.formatData(this.data.fields,date),
      choseData:this.data.choseData,
      date:this.data.date,
      icon:this.data.icon
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  onPullDownRefresh() {
    wx.showNavigationBarLoading()
    this.data.pageNum = 1
    setTimeout(() => {
      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh()
    }, 1000);
  },
  onReachBottom() {
    this.data.pageNum += 1

  },


  // 按日期 年度/月度/今日 查询
  tabClick(e){
    let propertyName = "checked"
    this.data.tab.forEach(item=>{
      if(item.index==e.target.id){
        item[propertyName]=true;
        this.data.fields = item.fields
      }else 
      item[propertyName]=false
    });
    if(this.data.fields == "day"){
      this.data.date = util.datetime(this.data.today)
    }
    let data = this.formatData(this.data.fields,this.data.date)
    this.data.choseData = data
    this.data.date = util.datetime(this.data.date)
    this.setData({
      tab:this.data.tab,
      fields:this.data.fields,
      choseData:this.data.choseData,
      date:this.data.date,
      startDate:this.data.date,
      endDate:this.data.date,
      VstartDate:util.formatData(this.data.fields,this.data.startDate),
      VendDate:util.formatData(this.data.fields,this.data.endDate),
    })
    this.loadSummary()
  },

  bindStartDate(e){
    this.data.startDate = e.detail.value
    this.data.VstartDate = this.formatData(this.data.fields,e.detail.value)
    this.setData({
      startDate:this.data.startDate,
      VstartDate:this.data.VstartDate
    })
  },
  bindEndDate(e){
    this.data.endDate = e.detail.value
    this.data.VendDate = this.formatData(this.data.fields,e.detail.value)
    this.setData({
      endDate:this.data.endDate,
      VendDate:this.data.VendDate
    })
    this.loadSummary()
  },

    //按日期查询 选择日期
    bindDateChange(e){
      this.data.choseData = this.formatData(this.data.fields,e.detail.value)
      this.data.date = e.detail.value
      this.setData({
        choseData:this.data.choseData,
        date:this.data.date
      })
      this.loadSummary()
    },
  //查询类型选择 1.收入 2.支出
  typeClick(e){
    this.setData({
      type:e.target.id,
      currTabName:e._relatedInfo.anchorTargetText
    })
    this.loadSummary()
  },
  loadSummary() {
    this.data.detailedList = []
    this.data.amount = 0
    let list ={
      type: this.data.type,
      pageSize: this.data.pageSize,
      pageNum: this.data.pageNum
    }
    if(this.data.fields == "day"){
      list['startDate'] = this.data.startDate;
      list['endDate'] = this.data.endDate;
    }else if(this.data.fields == "month"){
      list['month'] = this.data.date.substr(0,7);
    }
    else list['year'] = this.data.date.substr(0,4);
    summaryList(list).then(res => {
      if (res.data[0] != null) {
        if(this.data.type==1){
          res.data.forEach(item => {
            let amount = (item.credit-item.debit)/100
            amount = amount.toFixed(2)
            item['amount'] = amount
            this.data.amount = (Number(this.data.amount) +Number(amount)).toFixed(2)
            this.data.detailedList.push(item)
          })
          this.data.total = this.data.detailedList.length
        }else if(this.data.type==2){
          res.data.forEach(item => {
          let amount = (item.debit-item.credit)/100
          amount = amount.toFixed(2)
            item['amount'] = amount
            this.data.amount = (Number(this.data.amount) +Number(amount)).toFixed(2)
            this.data.detailedList.push(item)
        })
        this.data.total = this.data.detailedList.length
        }
      }else{
        this.data.total = 0
        this.data.amount = 0
      }
      this.setData({
        total:this.data.total,
        amount:this.data.amount,
        detailedList:this.data.detailedList
      })
    })
  },

    //格式化时间 
    formatData:function (type,date,) {
      var date = new Date(date);
      var year = date.getFullYear()
      var month = date.getMonth() + 1
      var day = date.getDate()
      var D =year+"年"+month+'月'+day+"日"
      var M =year+"年"+month+'月'
      var Y =year+"年"
      // [year, month, day].map(formatNumber).join('-')
      switch(type){
        case "day":return D;
        break;
        case "month":return M;
        break;
        case "year":return Y;
        break;
        case "-":[year, month].map(formatNumber).join('-')
      }
    }

})